<?php

namespace App\Http\Controllers;

use App\Exceptions\AddressException;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;


use App\Interfaces\AddressService;


use Auth,Log;


class MyAddressController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(AddressService $addressService)
    {

        $user = Auth::user();

        if($user){
            $user_id = $user->id;

        }else{

            throw new AddressException("收货人参数不能为空，请登录");
        }//end if()

        $addressInfo = $addressService->getAddressInfo($user_id);

        return view('mobile.my.address', compact('addressInfo'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Requests\AddressRequest $addressRequest, AddressService $addressService)
    {

        $user = Auth::user();

        if($user){
            $user_id = $user->id;

        }else{

            throw new AddressException("收货人参数不能为空，请登录");
        }//end if()


        $addressArr = [];

        $addressArr = $addressRequest->all();

        if(empty($addressArr)){

            throw new AddressException("要创建的收货地址字段信息不能为空");

        }//end if()


        $address_id = $addressService->addAddress($user_id, $addressArr);

        return Response()->json(['status'=>1, 'address_id'=>$address_id]);
    }

    /**
     * 新增收货地址.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    // public function store(Request $request)
    public function store(Requests\AddressRequest $addressRequest, AddressService $addressService)
    {

        $user = Auth::user();

        if($user){
            $user_id = $user->id;

        }else{

            throw new AddressException("收货人参数不能为空，请登录");
        }//end if()


        $addressArr = [];

        $addressArr = $addressRequest->all();

        if(empty($addressArr)){

            throw new AddressException("要创建的收货地址字段信息不能为空");

        }//end if()


        $address_id = $addressService->addAddress($user_id, $addressArr);

        return Response()->json(['status'=>1, 'address_id'=>$address_id]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    /*public function show($id)
    {
        //
    }*/
    public function show($id, AddressService $addressService)
    {

        $user = Auth::user();

        if($user){
            $user_id = $user->id;

        }else{

            throw new AddressException("收货人参数不能为空，请登录");
        }//end if()

        $addressInfo = $addressService->getAddressInfo($user_id);

        return view('mobile.my.address_create', compact('addressInfo'));
    }

    /**
     * 编辑收货地址页面.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($address_id, Requests\UpdateAddressRequest $updateAddressRequest, AddressService $addressService)
    {

        if(empty($address_id)){

            throw new AddressException("要修改的收货地址id不能为空");

        }//end if()

        //根据地址id获取地址信息
        /*$addressArr = $updateAddressRequest->all();
        if(empty($addressArr)){

            throw new AddressException("要修改的收货地址信息字段不能为空");

        }//end if()

        $addressObj = $addressService->updateAddress($address_id, $addressArr);*/

        $addressInfo = $addressService->getAddress($address_id);

        if (empty($addressInfo)) {

            throw new AddressException("要修改的地址信息不存在");
        }

        return view('mobile.my.address_edit', compact('addressInfo'));
    }

    /**
     * 修改收货地址
     *
     * @param int $address_id     收货地址id
     * @param array $addressArr   要修改的收货地址信息数组
     * @return \Illuminate\Http\Response
     */
    public function update($address_id, Requests\UpdateAddressRequest $updateAddressRequest, AddressService $addressService)
    {

        if(empty($address_id)){

            throw new AddressException("要修改的收货地址id不能为空");

        }//end if()

        $addressArr = $updateAddressRequest->all();
        if(empty($addressArr)){

            throw new AddressException("要修改的收货地址信息字段不能为空");

        }//end if()

        $addressObj = $addressService->updateAddress($address_id, $addressArr);

        return Response()->json(['status'=>1]);

    }

    /**
     * 删除收货地址
     *
     * @param $address_id      收货地址id
     * @return \Illuminate\Http\Response
     */
    public function destroy($address_id, AddressService $addressService)
    {

        if(empty($address_id)){

            throw new AddressException("要删除的收货地址id不能为空");

        }//end if()

        $addressObj = $addressService->removeAddress($address_id);

        return Response()->json(['status'=>1]);
    }
}
